package com.pig4cloud.pig.common.core.wx;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.common.core.util.OkHttpClientUtil;

import java.io.IOException;


public class WxMessage {

    /**
     * 获取ACCESSTOKEN
     * @param APPID
     * @param APPSECRET
     * @return
     */
    public static String getACCESSTOKEN(String APPID,String APPSECRET){
        String access_token = "";
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
        String result = WXCommonUtil.httpsRequest(requestUrl, "GET","");
        System.out.println("获取accessToken " + result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            access_token = jsonObject.containsKey("access_token") ? jsonObject.getStr("access_token"): "";
        }
        return access_token;
    }

    public static JSONObject getUserMpInfo(String ACCESS_TOKEN, String OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ACCESS_TOKEN+"&openid="+OPENID+"&lang=zh_CN";
        String result = WXCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }


    public static JSONObject getUserMp(String ACCESS_TOKEN, String NEXT_OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+ACCESS_TOKEN+"&next_openid="+NEXT_OPENID;
        String result = WXCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    public static JSONObject sendMsg(String access_token, String param){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
//        String result = WXCommonUtil.httpsRequest(requestUrl, "POST", param);
		try {
			String res = OkHttpClientUtil.getInstance().postJson(requestUrl,param);
			JSONObject jsonObject = JSONUtil.parseObj(res);
			return jsonObject;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
    }

    /**
     * 根据模板消息的 short_id  获取 模板的id
     * @param template_id_short
     * @return
     */
    public String getTemplateId(String template_id_short,String ACCESS_TOKEN){
        String template_id = "";

        String requestUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token="+ACCESS_TOKEN;
        String param = "{\"template_id_short\":\""+template_id_short+"\"}";

        String result = WXCommonUtil.httpsRequest(requestUrl, "POST",param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            int errcode = jsonObject.getInt("errcode");
            String errmsg = jsonObject.getStr("errmsg");
            template_id = jsonObject.containsKey("template_id") ? jsonObject.getStr("template_id"): "";
        }
        return template_id;
    }


    public static void main(String[] args){

        String accessToken = getACCESSTOKEN("wx4023ebd185405f69","d2b3a8f37a51dd902316ecf0a0ac0711");
        String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;

        System.out.println("accessToken:" + accessToken);
        JSONObject jsonObject = getUserMp(accessToken,"");
        if(jsonObject!=null){
            System.out.println(jsonObject.toString());
        }
    }
}
